package com.wh.usercenter3.util;

import com.wh.usercenter3.constant.CodeConstant;
import com.wh.usercenter3.exception.BaseException;
import com.wh.usercenter3.pojo.User;
import com.wh.usercenter3.pojo.vo.NewUserVo;
import org.springframework.beans.BeanUtils;

import javax.servlet.http.HttpServletRequest;

import static com.wh.usercenter3.constant.UserConstant.LOGIN_USER;

/**
 * 有关user实体的封装类工具
 */

public class UserUtil {
    /**
     * 数据脱敏
     * @param user
     * @return
     */
    public static NewUserVo userToVo(User user) {
        NewUserVo newUserVo = new NewUserVo();
        BeanUtils.copyProperties(user,newUserVo);
        return newUserVo;
    }

    /**
     * 返回当前用户信息
     * @param request
     * @return
     */
    public static User userByLogin(HttpServletRequest request) {

        Object user = request.getSession().getAttribute(LOGIN_USER);
        if (user == null) {
            throw new BaseException(CodeConstant.CLIENT_ERR, "未登录");
        }
        return (User) user  ;

    }

    /**
     *  判断当前传入用户是不是管理员
     * @param user
     * @return
     */
    public static Boolean isAdmin(User user) {
        return user.getUserRole() == 1;
    }


}
